package com.gameShare.service.game

import com.gameShare.entity.dto.game.GameCommentDto
import com.gameShare.entity.dto.user.UserCenterCommentDto
import com.gameShare.entity.vo.PageVo
import com.gameShare.enums.misc.MessageTypeEnum
import com.gameShare.enums.misc.ResponseCodeEnum
import com.gameShare.enums.user.UserActionEnum
import com.gameShare.exceptions.BusinessException
import com.gameShare.mappers.game.GameInfoMapper
import com.gameShare.mappers.misc.GameCommentMapper
import com.gameShare.service.misc.MessageService
import com.gameShare.service.user.UserActionService
import org.springframework.stereotype.Service
import java.util.Date

@Service
class GameCommentService(
    private val gameInfoService: GameInfoService,
    private val gameCommentMapper: GameCommentMapper,
    private val messageService: MessageService,
    private val userActionService: UserActionService,
    private val gameInfoMapper: GameInfoMapper
) {


    /**
     * 获取一个游戏下所有评论信息
     */
    fun getGameTreeCommentPage(gameId : String, pageNo : Int?, pageSize : Int?): PageVo<GameCommentDto?> {

        gameInfoService.checkGameInfoExist(gameId)

        return PageVo.generatePageVo(
            pageNo,
            pageSize,
            { gameCommentMapper.selectTopCommentCountByGameId(gameId) },
            { start, end -> gameCommentMapper.selectCommentByGameId(gameId, start, end) }
        )
    }

    /**
     * 发送游戏评论
     *
     * @param gameId
     * @param userId
     * @param pCommentId
     * @param content
     */
    fun postGameComment(gameId : String, userId : String, pCommentId : Int?, content : String){
        gameInfoService.checkGameInfoExist(gameId)

        // 检查是否存在pCommentId在这个gameId里面
        // 发送回复消息给用户
        pCommentId?.let {
            checkPCommentExist(pCommentId, gameId)
            messageService.sendMessage(
                userId,
                gameCommentMapper.selectPCommentUserId(pCommentId),
                gameId,
                MessageTypeEnum.REPLY_MESSAGE.code,
                content
            )
        }
	// TODO 向游戏发布用户发送消息
        messageService.sendMessage(
            userId,
            gameInfoMapper.selectUserIdByGameId(gameId),
            gameId,
            MessageTypeEnum.GAME_COMMENT_MESSAGE.code,
            content
        )
        // 添加用户评论数量
        userActionService.updateUserActionCount(userId, UserActionEnum.COMMENT.code, 1)
        gameCommentMapper.insertGameComment(gameId, userId, pCommentId, content, Date())
    }

    /**
     * 获取用户评论页
     *
     * @param userId 用户id
     * @param pageNo 页码
     * @param pageSize 页面大小
     * @return 用户评论页面vo对象
     */
    fun getUserCommentPage(userId: String, pageNo : Int?, pageSize: Int?) : PageVo<UserCenterCommentDto?> {
        return PageVo.generatePageVo(
            pageNo,
            pageSize,
            {gameCommentMapper.selectUserCenterCommentCount(userId)},
            {start, end ->  gameCommentMapper.selectUserCenterComment(userId, start, end)}
        )
    }


    /**
     * 检查用户评论是否在该游戏内
     *
     * @param gameId
     * @param commentId
     */
    fun checkCommentInGame(gameId: String, commentId : Int){
        if (gameCommentMapper.checkCommentInGame(gameId, commentId) == 0)
            throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR)
    }

    /**
     * 检查是否存在父评论
     *
     * @param pCommentId 父评论id
     * @param gameId 游戏id
     */
    private fun checkPCommentExist(pCommentId : Int, gameId: String) {
        val pCommentIdList = gameCommentMapper.selectPCommentIdByGameId(gameId)
        if (pCommentIdList.isEmpty() || !pCommentIdList.contains(pCommentId)){
            throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR)
        }
    }

    // TODO 删评

}
